package cn.qn.controller.member;

import cn.qn.base.PageVO;
import cn.qn.dto.ResponseDTO;
import cn.qn.dto.member.MemberAccountDTO;
import cn.qn.dto.member.MemberAccountQueryDTO;
import cn.qn.dto.security.MemberDTO;
import cn.qn.dto.security.MenuDTO;
import cn.qn.exceptions.BizException;
import cn.qn.framwork.security.MemberAccountUtils;
import cn.qn.moudle.security.MemberAccount;
import cn.qn.service.member.MemberService;
import cn.qn.service.security.MenuService;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author: Bryant
 * @date: 2019/5/28
 * @description:
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference(check = false)
    private MenuService menuService;
    @Reference(check = false)
    private MemberService memberService;

    @RequestMapping(value = "/menus", name = "通过用户userId查询其菜单", method = RequestMethod.GET)
    public ResponseDTO findLoginUerMenus() {
        MemberAccount memberAccount = MemberAccountUtils.getCurrentMember();
        if (StringUtils.isBlank(memberAccount.getUserId())) {
            return ResponseDTO.error("用户ID为空");
        }
        Set<MenuDTO> menus = menuService.findMenusByMemberId(memberAccount.getUserId());
        return ResponseDTO.success(menus);
    }

    @RequestMapping(value = "/list" ,name="员工列表",method = RequestMethod.GET)
    public ResponseDTO page(MemberAccountQueryDTO query){
        PageVO<MemberAccountDTO> members = memberService.pageQueryMembers(query);
        return ResponseDTO.success(members);
    }

    @RequestMapping(value = "/findByUserId/{userId}" ,method = RequestMethod.GET)
    public ResponseDTO findByUserId(@PathVariable String userId){
        if(StringUtils.isBlank(userId)){
            return ResponseDTO.error("登录账号不能为空");
        }
        MemberAccount member = memberService.findMemberAccountByUserId(userId);
        if(null!=member){
            return ResponseDTO.success(true,"登录账号已存在");
        }
        return ResponseDTO.success(false);
    }

    /**
     * 修改员工
     * @return
     */
    @ApiOperation(value="新增员工",notes="新增员工",produces = MediaType.APPLICATION_JSON_VALUE,response =Integer.class )
    @RequestMapping(value = "/insert" ,method = RequestMethod.POST)
    public ResponseDTO save(@RequestBody MemberAccountDTO member){
        try {
            MemberAccount account=memberService.saveMemberAccount(member);
            return ResponseDTO.success(account,"新增成功");
        }catch (BizException e){
            return ResponseDTO.error(e.getMessage());
        }
    }

    /**
     * 修改员工
     * @return
     */
    @ApiOperation(value="修改员工",notes="修改员工",produces = MediaType.APPLICATION_JSON_VALUE,response =Integer.class )
    @RequestMapping(value = "/update" ,method = RequestMethod.POST)
    public ResponseDTO update(@RequestBody MemberAccountDTO member){
        try {
            MemberAccount account=memberService.updateMemberAccount(member);
            return ResponseDTO.success(account,"修改成功");
        }catch (BizException e){
            return ResponseDTO.error(e.getMessage());
        }
    }

    /**
     * 查询手机号码是否存在
     * @return
     */
    @RequestMapping(value = "/exist/mobile/{mobile}" ,name="查询手机号码是否存在",method = RequestMethod.GET)
    public ResponseDTO existMobile(@PathVariable String mobile){
        if(StringUtils.isBlank(mobile)){
            return ResponseDTO.error("手机号不能为空");
        }
        MemberAccount member = memberService.findMemberAccountMobile(mobile);
        if(member!=null){
            return ResponseDTO.success(member,"该手机号已经被注册!");
        }else{
            return ResponseDTO.error("不存在");
        }
    }

    /**
     * 重置密码
     * @return
     */
    @ApiOperation(value="重置密码",notes="重置密码",produces = MediaType.APPLICATION_JSON_VALUE,response =Boolean.class )
    @RequestMapping(value = "/password/reset" ,method = RequestMethod.POST)
    public ResponseDTO resetPassword(@RequestBody MemberAccountDTO member){
        try {
            memberService.updatePassword(member);
            return ResponseDTO.success(true,"重置密码成功");
        }catch (BizException e){
            return ResponseDTO.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/update/status" ,name="修改用户状态",method = RequestMethod.POST)
    public ResponseDTO updateStatus(@RequestBody MemberAccountDTO member){
        try {
            memberService.updateStatus(member);
            return ResponseDTO.success(true,"操作成功");
        }catch (BizException e){
            return ResponseDTO.error(e.getMessage());
        }
    }
}
